package cn.lili.controller.user;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.SwitchEnum;
import cn.lili.common.security.OperationalJudgment;
import cn.lili.common.security.enums.AuthUserFieldEnum;
import cn.lili.common.security.enums.ObjectFieldEnum;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.MemberEvaluation;
import cn.lili.modules.member.entity.dto.EvaluationQueryParams;
import cn.lili.modules.member.entity.dto.MemberEvaluationDTO;
import cn.lili.modules.member.entity.vo.EvaluationNumberVO;
import cn.lili.modules.member.entity.vo.MemberEvaluationListVO;
import cn.lili.modules.member.entity.vo.MemberEvaluationVO;
import cn.lili.modules.member.service.MemberEvaluationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 会员商品评价接口
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@RestController
@Api(tags = "会员商品评价接口")
@RequestMapping("/user/evaluation")
public class UserEvaluationController {
    @Autowired
    private MemberEvaluationService memberEvaluationService;

    @PreventDuplicateSubmissions
    @ApiOperation(value = "通过id获取评论")
    @ApiImplicitParam(name = "id", value = "评价ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<MemberEvaluationVO> get(@PathVariable String id) {

        return ResultUtil.data(memberEvaluationService.queryById(id));
    }

    @ApiOperation(value = "查看某一个商品的评价列表")
    @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/show")
    public ResultMessage<Page<MemberEvaluationListVO>> queryGoodsEvaluation(EvaluationQueryParams evaluationQueryParams) {
        //设置查询查询商品
        evaluationQueryParams.setStatus(SwitchEnum.OPEN.name());
        return ResultUtil.data(memberEvaluationService.queryPage(evaluationQueryParams));
    }

    @ApiOperation(value = "查看某一个商品的评价数量")
    @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/number/{goodsId}")
    public ResultMessage<EvaluationNumberVO> queryEvaluationNumber(@NotNull @PathVariable("goodsId") String goodsId) {
        return ResultUtil.data(memberEvaluationService.getEvaluationNumber(goodsId));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "添加会员评价")
    @PostMapping
    public ResultMessage<MemberEvaluationDTO> save(@Valid MemberEvaluationDTO memberEvaluationDTO) {
        return ResultUtil.data(memberEvaluationService.addMemberEvaluation(memberEvaluationDTO, true));
    }

    @ApiOperation(value = "获取评价分页")
    @GetMapping
    public ResultMessage<Page<MemberEvaluationListVO>> getByPage(EvaluationQueryParams evaluationQueryParams) {

        return ResultUtil.data(memberEvaluationService.queryPage(evaluationQueryParams));
    }

    @ApiOperation(value = "我的评价列表：店铺或者供应商，则根据当前用户信息查看")
    @GetMapping("/mine")
    public ResultMessage<Page<MemberEvaluation>> mine(EvaluationQueryParams evaluationQueryParams) {

        return ResultUtil.data(memberEvaluationService.mineEvaluation(evaluationQueryParams));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "修改评价状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "评价ID", required = true, paramType = "path"), @ApiImplicitParam(name = "status",
            value = "显示状态,OPEN 正常 ,CLOSE 关闭", required = true, paramType = "query")})
    @GetMapping(value = "/update/status/{id}")
    public ResultMessage<Object> updateStatus(@PathVariable String id, @NotNull String status) {
        OperationalJudgment.judgment(memberEvaluationService.queryById(id));
        memberEvaluationService.updateStatus(id, status);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除评论")
    @ApiImplicitParam(name = "id", value = "评价ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping(value = "/{id}")
    public ResultMessage<Page<Object>> delete(@PathVariable String id) {
        OperationalJudgment.judgment(memberEvaluationService.queryById(id));
        memberEvaluationService.delete(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "回复评价")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "评价ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "reply", value = "回复内容", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name =
            "replyImage", value = "回复图片", dataType = "String", paramType = "query")})
    @PutMapping(value = "/reply/{id}")
    public ResultMessage<MemberEvaluationVO> reply(@PathVariable String id, @RequestParam String reply, @RequestParam String replyImage) {
        OperationalJudgment.judgment(memberEvaluationService.queryById(id), ObjectFieldEnum.STORE_ID, AuthUserFieldEnum.EXTEND_ID);
        memberEvaluationService.reply(id, reply, replyImage);
        return ResultUtil.success();
    }

}
